package com.sinobpo.rnd2.ehr.web.action;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import com.sinobpo.rnd2.ehr.model.User;
import com.sinobpo.rnd2.ehr.util.UConst;
import com.sinobpo.rnd2.ehr.util.UFunc;

/**
 * 
 * @author weida.ni
 * @date Jul 31, 2012
 */
public class LogonAction extends BaseAction {
	/**
	 * sss
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() {
		JSONObject sJson = new JSONObject();
		String sMessage = UConst.INVALID_OPRTORPWD;
		try{
			String sNo = user.getNo();
			String sPassword = user.getPassword();
			if(UFunc.IsNullString(sNo)||UFunc.IsNullString(sPassword)){
				sJson.put("message", sMessage);
		    	sJson.put("success", false);
		    	setMessage(sJson);
			}else{
				//验证用户
				String  sSQL= "SELECT NO,NAME FROM OPERATOR WHERE NO='"+sNo+"' AND PASSWORD='"+sPassword+"'";
			    List<User> list=simpleJdbcTemplate.getJdbcOperations().query(sSQL,
			    		   new BeanPropertyRowMapper(User.class));
				//设置返回结果
			    if(list.size()==0){//验证失败
			    	sJson.put("message", sMessage);
			    	sJson.put("success", false);
			    	setMessage(sJson);
			    }else{
			    	saveUsers(list.get(0));
			    	request.getSession().setAttribute("logon", list.get(0).getName());
			    	sJson.put("requestUri", "index.jsp");
			    	sJson.put("success", true);
			    	setMessage(sJson);
			    }
			}
		}catch (Exception e) {
			sJson.put("message", "数据库连接异常");
	    	sJson.put("success", false);
	    	setMessage(sJson);
		}
	    	
		return SUCCESS;
	}
	
	
	/*
	 * 退出登录
	 */
	public String logout(){
		JSONObject sJson = new JSONObject();
		if(oldUser!=null){
			reset();
			sJson.put("requestUri", "login.jsp");
		}
		sJson.put("success", false);
    	setMessage(sJson);
		return SUCCESS;
	}

	
}
